<template>
  <FullPage class="">
    <VGrid
      ref="vgrid"
      :formConfig="formConfig"
      :tableConfig="tableConfig"
      :proxy="proxy"
      @handleFormBtnClick="handleFormBtnClick"
      @handleTableBtnClick="handleTableBtnClick"
    ></VGrid>
    <RoleFormDialog ref="roleFormDialog" @submit="getTableData"></RoleFormDialog>
    <RolePrivilegeDialog ref="rolePrivilegeDialog"></RolePrivilegeDialog>
  </FullPage>
</template>

<script>
import FullPage from '@/components/FullPage';
import VGrid from '@/components/VGrid';
import RoleFormDialog from './RoleFormDialog';
import RolePrivilegeDialog from './RolePrivilegeDialog';

export default {
  components: {
    FullPage,
    VGrid,
    RoleFormDialog,
    RolePrivilegeDialog,
  },
  data() {
    return {
      formConfig: {
        props: {
          inline: true,
        },
        btns: [
          {
            key: 'submit',
            label: '搜索',
          },
          {
            key: 'reset',
            label: '重置',
          },
          {
            key: 'addRole',
            label: '新增角色',
            type: 'success',
            icon: 'el-icon-plus',
          },
        ],
        fieldList: [
          {
            type: 'input',
            model: 'name',
            label: '',
            props: {
              placeholder: '请输入角色名',
            },
          },
        ],
      },
      tableConfig: {
        props: {
          border: true,
          stripe: true,
        },
        columnList: [
          {
            prop: 'name',
            label: '角色名',
          },
          {
            prop: 'desc',
            label: '角色描述',
          },
          {
            label: '操作',
            type: 'operation',
            btns: [
              {
                key: 'rolePrivilege',
                label: '管理角色权限',
              },
              {
                key: 'edit',
                label: '编辑',
              },
              {
                key: 'delete',
                label: '删除',
              },
            ],
          },
        ],
      },
      proxy: {
        list: {
          api: this.$api.account.role.getRoleList,
        },
        delete: {
          api: this.$api.account.role.deleteRole,
        },
      },
    };
  },

  methods: {
    handleFormBtnClick(key, params) {
      if (key === 'addRole') {
        this.$refs.roleFormDialog.show('add');
      }
    },
    handleTableBtnClick(key, row) {
      if (key === 'edit') {
        this.$refs.roleFormDialog.show('edit', row);
      }

      if (key === 'rolePrivilege') {
        this.$refs.rolePrivilegeDialog.show(row);
      }
    },

    getTableData() {
      this.$refs.vgrid.getTableData();
    },

    refreshTableData() {
      this.$refs.vgrid.refreshTableData();
    },
  },
};
</script>

<style lang="scss" scoped></style>
